import { ReadWriteProxy } from "../../core/utils/read_write_proxy";
import SaveGameWorker from '../../workers/savegame.worker'
import { globalConfig } from "../../config/global";

export class SaveGame extends ReadWriteProxy{
  constructor(app, filename){
    super(app, filename)

    this.time = null;

    this.worker = new SaveGameWorker();
  }

  getDefaultData(){
    abstract;
  }

  getData(){
    return Object.assign(this.getDefaultData(), this.currentData);
  }

  initialize(){
    return this.readAsync()
  }

  serializeToWrite(data){
    // 保存防抖，多次相同保存将只有最后一次生效
    return new Promise(resolve => {
      clearTimeout(this.time);
      this.time = setTimeout(() => {
        this.currentData = data;
    
        this.workerToWrite().then(() => {
          resolve();
        })
      }, globalConfig.savegameMsTime);
    })
  }

  deleteAllData(){
    return this.deleteAsync()
  }
}